// 为城市近些年旅游人数插入随机数据:
const allLines = require('../schema/allLines')
const citys = require('../schema/citys')
const {log} = require('debug');

async function random() {
    const res = await allLines.findAll({
        attributes: ['id', 'navigation_bar'],
        raw: true
    })
    let cityList = []
    res.forEach((item, index) => {
        if (cityList.indexOf(item.navigation_bar.split(' > ')[3]) != -1) {
        } else {
            cityList.push(item.navigation_bar.split(' > ')[3])
        }
        /* console.log(item.navigation_bar.split(' > ')[3]);
         allLines.update({
             provinceName: item.navigation_bar.split(' > ')[3]
         }, {
             where: {
                 id: item.id
             }
         })
             .then(res => {
                 console.log('成功')
             })
             .catch(e => {
                 console.log(e)
                 console.log('失败')
             })*/
    })
    // console.log(cityList)

    cityList.forEach((item, index) => {
        console.log(item)
        citys.create({
            name: item,
            number_1: randomNum(1500, 9000),
            number_2: randomNum(3000, 16000),
            number_3: randomNum(1200, 8000),
            number_4: randomNum(1200, 16000),
            number_5: randomNum(6000, 10000)
        })
            .then(res => {
                    console.log('成功')
                }
            )
            .catch(e => {
                console.log(e)
                console.log('失败')
            })
    })
}

function randomNum(minNum, maxNum) {
    switch (arguments.length) {
        case 1:
            return parseInt(Math.random() * minNum + 1, 10);
            break;
        case 2:
            return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
            break;
        default:
            return 0;
            break;
    }
}

random();
// const list = ['你好', '武汉', '云南', '贵州', '山西']
// console.log(list.indexOf('武汉2'))